import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Layout from '../views/Layout.vue'
import Attendance from '../views/Attendance.vue'
import Overtime from '../views/Overtime.vue'
import Leave from '../views/Leave.vue'
import LeaveAdjustment from '../views/LeaveAdjustment.vue'
import Salary from '../views/Salary.vue'
import TeamSalary from '../views/TeamSalary.vue'
import UserManage from '../views/UserManage.vue'
import ApprovalCenter from '../views/ApprovalCenter.vue'
import OvertimeApprove from '../views/OvertimeApprove.vue'
import SubordinateAttendance from '../views/SubordinateAttendance.vue'
import AttendanceStatistics from '../views/AttendanceStatistics.vue'
import AttendanceException from '../views/AttendanceException.vue'
import LeaveStatistics from '../views/LeaveStatistics.vue'
import LeaveBalance from '../views/LeaveBalance.vue'
import Settings from '../views/Settings.vue'
import NotificationCenter from '../views/NotificationCenter.vue'
import Dashboard from '../views/Dashboard.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    redirect: '/attendance',
    children: [
      {
        path: '/attendance',
        name: 'Attendance',
        component: Attendance
      },
      {
        path: '/attendance/overtime',
        name: 'Overtime',
        component: Overtime
      },
      {
        path: '/attendance/approve',
        name: 'OvertimeApprove',
        component: OvertimeApprove
      },
      {
        path: '/attendance/subordinate',
        name: 'SubordinateAttendance',
        component: SubordinateAttendance
      },
      {
        path: '/leave',
        name: 'Leave',
        component: Leave
      },
      {
        path: '/leave/adjustment',
        name: 'LeaveAdjustment',
        component: LeaveAdjustment
      },
      {
        path: '/leave/statistics',
        name: 'LeaveStatistics',
        component: LeaveStatistics
      },
      {
        path: '/leave/balance',
        name: 'LeaveBalance',
        component: LeaveBalance
      },
      {
        path: '/salary',
        name: 'Salary',
        component: Salary
      },
      {
        path: '/salary/team',
        name: 'TeamSalary',
        component: TeamSalary
      },
      {
        path: '/approval',
        name: 'ApprovalCenter',
        component: ApprovalCenter
      },
      {
        path: '/attendance/statistics',
        name: 'AttendanceStatistics',
        component: AttendanceStatistics
      },
      {
        path: '/attendance/exception',
        name: 'AttendanceException',
        component: AttendanceException
      },
      {
        path: '/user',
        name: 'UserManage',
        component: UserManage
      },
      {
        path: '/settings',
        name: 'Settings',
        component: Settings
      },
      {
        path: '/notification',
        name: 'NotificationCenter',
        component: NotificationCenter
      },
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    if (token) {
      // 根据角色重定向到对应页面
      const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
      if (userInfo.role === 'OTHER') {
        next('/settings')
      } else {
      next('/')
      }
    } else {
      next()
    }
  } else {
    if (token) {
      // 如果访问根路径，根据角色重定向
      if (to.path === '/') {
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
        if (userInfo.role === 'OTHER') {
          next('/settings')
        } else {
          next('/attendance')
        }
      } else {
      next()
      }
    } else {
      next('/login')
    }
  }
})

export default router

